#include "mdiareaplayer.h"
#include <QtDeclarative/QDeclarativeView>
#include <QtDeclarative/QDeclarativeContext>
#include <QPoint>
#include <QMdiSubWindow>
#include <QDebug>
#include <QResizeEvent>

MdiAreaPlayer::MdiAreaPlayer(QString appHome, MPlayerWrapper * player, QWidget *parent):
    QMdiArea(parent)
{

    this->refreshWTimer = new QTimer(this);
    this->refreshWTimer->setSingleShot(true);

    this->progressBarTimer = new QTimer(this);
    this->progressBarTimer->setSingleShot(true);

    this->setWindowFlags(Qt::FramelessWindowHint);

    connect(refreshWTimer, SIGNAL(timeout()), this, SLOT(resizePerAspect()));
    connect(progressBarTimer, SIGNAL(timeout()), this, SLOT(hideProgressBar()));

    connect(player,SIGNAL(started(int,double)),this,SLOT(started(int,double)));

    QString qmlsrc = appHome +"/qml/detailVideo.qml";


    sw = new QMdiSubWindow(this,Qt::FramelessWindowHint);
    sw->setGeometry(40, 40,500,300);
    sw->show();


    swQml = new QMdiSubWindow(this);//,Qt::FramelessWindowHint);
    qmlViewer = new QDeclarativeView(swQml);
    qmlViewer->setResizeMode(QDeclarativeView::SizeViewToRootObject);
    //set controleur
    qmlViewer->rootContext()->setContextProperty("appControl", this);
    qmlViewer->rootContext()->setContextProperty("videoControl", player);

    qmlViewer->setSource(QUrl::fromLocalFile(qmlsrc));
    //this->grabKeyboard();
    swQml->hide();
}

void MdiAreaPlayer::hideProgressBar(){
    swQml->hide();
    sw->setFocus();
}

void MdiAreaPlayer::willHideProgressBar() {
    progressBarTimer->start(5000);
}

void MdiAreaPlayer::showProgressBar(){
    int h = qmlViewer->geometry().height();
    int w = qmlViewer->geometry().width();
    //qDebug() <<"ge h "<< h;
    //qDebug() <<"ge w "<< w;
    int thisH = this->geometry().height();
    int thisW = this->geometry().width();

    swQml->setGeometry((thisW-w)/2, thisH-h ,w,h);
    swQml->show();
    progressBarTimer->start(5000);

    swQml->setFocus();
    qmlViewer->setFocus();
}

void MdiAreaPlayer::started(int length, double aspect){
    qDebug()<< "nouvelAspect"<<aspect;
    this->mAspect = aspect;
    this->resizePerAspect();
}

void MdiAreaPlayer::resizePerAspect(){

    int  w = this->geometry().width();
    double dW = w;
    double dH = dW/this->mAspect;
    int h = dH;
    int y = this->geometry().height();
    y -= h;
    y=y/2;
    sw->setGeometry(0, y,w,h);
}

void MdiAreaPlayer::resizeEvent(QResizeEvent *resizeEvent){
    //qDebug()<<"resised";
    refreshWTimer->start(200);
}


void MdiAreaPlayer::keyPressEvent ( QKeyEvent * event ){

    qDebug()<<"key event:"<< event->text()<<" key:"<<event->key();
    if(Qt::Key_Return == event->key()
            || Qt::Key_Enter == event->key()){

        if(swQml->isHidden()){
            showProgressBar();
            event->accept();
            return;
        }

    }


    if(Qt::Key_Meta == event->key()
            || Qt::Key_Escape == event->key()
            ){
        emit(mPlayerKeyPress(event->key()));
        return;
    }

}
